/*
 * Copyright: Copyright (c)2011
 * Company: 易宝支付(YeePay)
 */

package com.cyl.yeepay.gateway.handler;

import com.cyl.h5.service.H5OrderService;
import com.cyl.yeepay.vo.PayNotifyRetVo;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import com.yeepay.yop.sdk.service.common.callback.YopCallback;

import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付成功回调
 * 
 */
@Component
public class YopPayCallbackHandler extends YopBaseCallbackHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(YopPayCallbackHandler.class);

    @Autowired
    private H5OrderService h5OrderService;
    
    @Override
    public String getType() {
        return "/pay/notify";
    }

    @Override
    public void handle(YopCallback callback) {
    	  String json = callback.getBizData();
    	  System.err.println(json);
    	  PayNotifyRetVo payNotifyRetVo = JSONUtil.toBean(json, PayNotifyRetVo.class);
          if(payNotifyRetVo==null) {
              throw new RuntimeException("订单支付回调异常 payNotifyRetVo is null");
          }
       	if(payNotifyRetVo.getOrderId() == null) {
              throw new RuntimeException("订单支付回调异常 payId is null");
      	}
        Long payId =   Long.parseLong(payNotifyRetVo.getOrderId());
       	String status = payNotifyRetVo.getStatus();
       	if(status==null) {
              throw new RuntimeException("订单支付回调异常 status is null");
       	}
       	Map<String, String> resultMap = new HashMap<String, String>();
  		if(h5OrderService.yeePayCallBack(payNotifyRetVo,json)) {
  			resultMap.put("result", "SUCCESS");
  		}else {
  	  		throw new RuntimeException("订单支付回调异常 yeePayCallBack false");
  		}
    }
}